package cn.edu.hunau.controller;


import cn.edu.hunau.pojo.Address;
import cn.edu.hunau.pojo.Car;
import cn.edu.hunau.pojo.Order;
import cn.edu.hunau.pojo.User;
import cn.edu.hunau.service.OrderService;
import cn.edu.hunau.service.impl.OrderServiceImpl;
import cn.edu.hunau.utils.JsonUtils;
import cn.edu.hunau.utils.PayLoadUtils;
import cn.edu.hunau.utils.R;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author 苏佳
 * @Date 2022 10 24 16 59
 **/
@WebServlet("*.order")
public class OrderController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String matchValue = req.getHttpServletMapping().getMatchValue();

        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("LOGIN_USER");
        OrderService orderService = new OrderServiceImpl();

        PrintWriter out = resp.getWriter();

        if("create".equals(matchValue)){
            //从请求数据中的payload中解析数据
            String data = PayLoadUtils.reader(req);

            Map<String, Object> maps = JsonUtils.stringToMap(data, String.class, Object.class);
            System.out.println(maps);
            List<Map<String,Object>> items = (ArrayList)maps.get("item");
            System.out.println(JsonUtils.objectToJson(items));
            //获得购物车的数据
            Car car = (Car)session.getAttribute("car");
            car=car.createNewCar(items);
            System.out.println(car);
            System.out.println(JsonUtils.objectToJson(car));
            try {
                Order o = orderService.createOrder(user, car);
                out.println(JsonUtils.objectToJson(R.ok().data(o)));
            } catch (Exception e) {
                //异常无法回显到客户端
                out.println(JsonUtils.objectToJson(R.error().message(e.getMessage())));
            }

        } else if ("pay".equals(matchValue)) {
            //从请求数据中的payload中解析数据
            String data = PayLoadUtils.reader(req);

            Map<String, Object> maps = JsonUtils.stringToMap(data, String.class, Object.class);
            System.out.println(maps);
            String oid = maps.get("oid").toString();

            Object object = maps.get("address");
            //FIX :理解为何要先转成String而不是直接调用Object.toString方法
            Address address = JsonUtils.jsonToPojo(JsonUtils.objectToJson(object), Address.class);
            System.out.println(address);
            orderService.payOrder(oid,address);
        }
        else if ("loadOrder".equals(matchValue)){
            String uidStr = req.getParameter("uid");
            int uid = Integer.parseInt(uidStr);

            List<Order> orders = orderService.loadOrder(uid);
            out.println(JsonUtils.objectToJson(R.ok().data(orders)));
        }
        else if("remove".equals(matchValue)){
            String id = req.getParameter("id");
            orderService.removeOrder(id);
            out.println(JsonUtils.objectToJson(R.ok().message("删除成功")));
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}

